<template>
  <div class="tableDownload">
    <div class="download_top">
      <div>
        <Select
          placeholder="请选择数据库表"
          style="width: 143px; height: 30px"
          transfer-class-name="dropdown">
          <Option
            v-for="item in dataList"
            :value="item.name"
            :key="item.type">{{ item.name }}
          </Option>
        </Select>
      </div>
      <DatePicker
        type="daterange"
        v-model="device_time"
        format="yyyy--MM-dd"
        @on-change="timechange"
        :transfer="true"
        placement="bottom-end"
        placeholder="请选择开始-结束时间"
        style="width: 276px; height: 30px"
      ></DatePicker>
      <div class="download_top_button">
        <Button
          type="primary"
          v-hasPermission="['25']"
          @click="addTaskList"
          style="
            margin-right: 23px;
            border-radius: 4px;
            font-size: 16px;
            border: rgba(0, 0, 0, 1) solid 1px;
          ">加入任务列表</Button>
        <Button
          type="primary"
          v-hasPermission="['25']"
          @click="downloadAll"
          style="
            margin-right: 20px;
            border-radius: 4px;
            font-size: 16px;
            border: rgba(0, 0, 0, 1) solid 1px;
          ">全部下载</Button>
      </div>
    </div>
    <div
      class="download_bottom"
      :style="{ height: `${height}px`, overflow: 'auto' }"
    >
      <div v-for="(v, i) in list" :key="i" class="download_bottom_list">
        <div class="download_bottom_left">
          <!-- <div class="uncheck"></div> -->
          <img
            src="../../assets/checked.svg"
            class="download_bottom_left_img"
            alt=""
          />
          <span download_bottom_left_text>{{ v.table_name }}</span>
        </div>
        <div class="download_bottom_right">
          <div class="download_bottom_data">
            {{ v.start_time }}至{{ v.end_time }}
          </div>
          <div class="download_bottom_button">
            <div class="download_button button" @click="download(v.id)">
              下载
            </div>
            <div class="download_delate button" @click="delate(v.id)">删除</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import dayjs from 'dayjs'
  import $ from 'jquery'
  export default {
    data () {
      return {
        dataList: [],
        model1: '',
        device_time: [],
        list: [],
        height: this.$store.state.height
      }
    },
    created () {
      this.getTableList()
      this.axios({
        method: 'post',
        url: 'info/get_table',
        params: {}
      }).then((res) => {
        this.dataList = res.data
      })
    },
    methods: {
      addTaskList () {
        console.log(this.model1)
        console.log(this.device_time)
        if (!this.model1) {
          this.$Message.error('表名不能为空')
          return
        } else if (!this.device_time[0] || !this.device_time[1]) {
          this.$Message.error('请选择开始时间或结束时间')
          return
        }
        this.axios({
          method: 'post',
          url: 'info/set_task',
          data: {
            table_name: this.model1,
            start_time: dayjs(this.device_time[0]).format('YYYY-MM-DD'),
            end_time: dayjs(this.device_time[1]).format('YYYY-MM-DD')
          }
        }).then((res) => {
          if (res.code === 1) {
            this.$Message.success('添加成功')
            this.getTableList()
          }
        })
      },
      getTableList () {
        this.axios({
          method: 'post',
          url: 'info/task_list',
          params: {}
        }).then((res) => {
          this.list = res.data
        })
      },
      downloadAll () {
        this.axios({
          method: 'post',
          url: 'info/down_load',
          data: { loading: true }
        }).then((res) => {
          console.log(res.data)
          res.data.forEach((item, index) => {
            const triggerDelay = 100
            const removeDelay = 1000
            this._createIFrame(item.url.url, index * triggerDelay, removeDelay)
          })
        })
      },
      _createIFrame (url, triggerDelay, removeDelay) {
        // console.log(url);
        // 动态添加iframe，设置src，然后删除
        setTimeout(function () {
          var frame = $(
            '<iframe style="display: none;" class="multi-download"></iframe>'
          )
          frame.attr('src', url)
          $(document.body).after(frame)
          setTimeout(function () {
            frame.remove()
          }, removeDelay)
        }, triggerDelay)
      },
      timechange () {},
      download (id) {
        this.axios({
          method: 'post',
          url: 'info/down_load_one',
          data: { id: id }
        }).then((res) => {
          if (res.data.url) {
            var frame = $(
              '<iframe style="display: none;" class="multi-download"></iframe>'
            )
            frame.attr('src', res.data.url)
            $(document.body).after(frame)
          }
        })
      },
      delate (id) {
        this.axios({
          method: 'post',
          url: 'info/task_del',
          data: { id: id }
        }).then((res) => {
          this.$Message.success(res.msg)
          this.getTableList()
        })
      }
    }
  }
</script>
<style scoped lang="scss">
  .tableDownload {
    width: 100%;
    height: 100%;
    // background: rgba(229, 229, 229, 1);
    padding: 39px 75px 0;
    .download_top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 40px;
      .ivu-select ivu-select-single ivu-select-default {
        width: 143px;
      }
    }
    .ivu-select-dropdown {
      width: 100px !important;
    }
    .download_bottom_list {
      // background: rgba(207, 194, 194, 0.94);
      margin-top: 10px;
      height: 61px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #ccc;
      .download_bottom_left {
        display: flex;
        margin-left: 4px;
        align-items: center;
        // .uncheck{
        //   width: 10px;
        //   height: 10px;
        //   border-radius: 5px;
        //   background: rgba(0, 0, 0, 1);
        // }
        .download_bottom_left_img {
          width: 10px;
          height: 10px;
          margin-right: 17px;
          margin-top: 2px;
        }
        .download_bottom_left_text {
          color: rgba(80, 80, 80, 1);
          font-size: 30px;
          line-height: 150%;
        }
      }
      .download_bottom_right {
        display: flex;
        justify-content: center;
        align-items: center;
        color: rgba(80, 80, 80, 1);
        font-size: 15px;
        line-height: 150%;
        .download_bottom_data {
          width: 207px;
        }
        .download_bottom_button {
          // padding: 6px 15px 24px;
          // box-sizing: border-box;
          padding-right: 24px;

          .button {
            width: 87px;
            height: 21px;
            border-radius: 2px;
            font-size: 8px;
            line-height: 21px;
            text-align: center;
            box-shadow: rgba(0, 0, 0, 1) solid 1px;
            border: rgba(0, 0, 0, 1) solid 1px;
          }
          .download_button {
            background-color: rgba(255, 255, 255, 1);
            color: rgba(0, 0, 0, 1);
          }
          .download_delate {
            color: rgba(255, 255, 255, 1);
            background-color: rgba(255, 87, 51, 1);
            margin-top: 7px;
          }
        }
      }
    }
  }

  .download_top > .ivu-btn {
    border-radius: 0px;
    box-shadow: rgba(0, 0, 0, 1) solid 1px;
    width: 120px;
    height: 32px;
    border-radius: 4px;
    font-size: 14px;
    line-height: 32px;
    text-align: center;
  }
</style>
